package com.neusoft.ysu.his.service.impl;

import com.neusoft.ysu.his.entity.CommonResult;
import com.neusoft.ysu.his.entity.Register;
import com.neusoft.ysu.his.mapper.RegisterMapper;
import com.neusoft.ysu.his.service.IRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

import static com.neusoft.ysu.his.util.JWTUtil.buildToken;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2023-08-29
 */
@Service
public class RegisterServiceImpl  implements IRegisterService {
    @Autowired
    RegisterMapper registerMapper;
    @Override
    public CommonResult findByRegnum(int regnum) {
        return new CommonResult<>(registerMapper.findByRegnum(regnum));
    }
    @Override
    public CommonResult findByName(int name) {return new CommonResult<>(registerMapper.findByName(name));}

    @Override
    public CommonResult loginCheck(Register register) throws UnsupportedEncodingException {
        System.out.println("register"+register);
        Register form_base=registerMapper.loginCheck(register);

        CommonResult result=new CommonResult<>(form_base);

        if(form_base!=null && form_base.getLoginname().equals(register.getLoginname()) && form_base.getPassword().equals(register.getPassword())
                && form_base.getDelmark()==0){
            result=new CommonResult<>(form_base);
            result.setMsg("登录成功！欢迎回来");
            HashMap<String,String> map=new HashMap<>();
            map.put("logname","admin");
            String token=buildToken(map);
            System.out.println("token:"+token);
            form_base.setToken(token);

            return result;
        }
        else {
            result.setCode(201);
            result.setMsg("用户名或密码不正确");
            return result;
        }


    }

    @Override
//    @Treansactional(rollbackFor = Exception.class)
    public CommonResult insert(Register register) {
        Integer x=0;
        String S_num="";
        LocalDateTime time=register.getCreatetime();
        S_num=(time.getYear()%100)+time.getMonth().getValue()+time.getDayOfMonth()+time.getHour()+time.getMinute()+time.getSecond()+"";
        x=Integer.parseInt(S_num);
        do {

            register.setRegnum(x);
            x++;
            System.out.println(register.getRegnum());
        }
        while(registerMapper.findByRegnum(register.getRegnum())!=null);

//        if(registerMapper.findByRegnum(register.getRegnum())!=null)
//        {
//            CommonResult commonResult=new CommonResult<>(null);
//            commonResult.setCode(400);
//            commonResult.setMsg("员工编号已存在");
//            return commonResult;
//        }
        int i=  registerMapper.insert(register);
        HashMap hashMap= new HashMap<>();
        hashMap.put("id",register.getId());
        hashMap.put("regnum",register.getRegnum());
        return new CommonResult<>(hashMap) ;
    }



    @Override
    public CommonResult findAll() {
        return new CommonResult<>(registerMapper.selectList(null));
    }
    @Override
    public CommonResult update(Register register) {
        System.out.println("数据库更新："+register);
        return new CommonResult<>(registerMapper.updateByregnum(register));
    }
    @Override
    public CommonResult deleteByRegnum(Register register) {
        System.out.println("删除数据："+register);
        System.out.println("h:"+registerMapper.deleteByregnum(register));
        return new CommonResult<>(registerMapper.deleteByregnum(register));
    }



}
